var app = angular.module('manage',[]);
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
function show(ele){
    ele.style.display = 'block'
}
function hide(ele){
    ele.style.display = 'none'
}
var articleIdArr = [];
app.controller('manageCon',function($scope,$http){
    $scope.logout = function(){
        $http.get('/logout').success(function(data){
            if(data['success']===true){
                console.log(data);
                window.location.href = '/admin';
            }
        })
    };
    $scope.showWrite = function(){
        show(document.querySelector('#write-post'));
        hide(document.querySelector('#edit-post'));
        hide(document.querySelector('#edit-comment'));
    };
    $scope.showEdit = function(){
        show(document.querySelector('#edit-post'));
        hide(document.querySelector('#write-post'));
        hide(document.querySelector('#edit-comment'));
    };
    $scope.showComment = function(){
        show(document.querySelector('#edit-comment'));
        hide(document.querySelector('#write-post'));
        hide(document.querySelector('#edit-post'));
    }

});
app.controller('postWrite',function($scope,$http){
    $scope.title = "";
    $scope.simIntro = "";
    $scope.content = "";
    $scope.kind = "";
    $scope.articleId = "";
    $scope.submit = function(title,simIntro,content,kind,articleId){
        var fd = new FormData();
        var imgFiles = document.querySelector('#showImg').files;
        var showImg = "";
        var url = "/article?articleId="+ articleId;
        if(imgFiles.length>0){
            showImg = "/upload/"+imgFiles[0].name;
            for(var i= 0;i<imgFiles.length;i++){
                fd.append("file",imgFiles[i]);
            }
            $http({
                url:"/postLoad",
                method:"post",
                data:fd,
                transformRequest: angular.identity,
                headers: {
                    "Content-Type": function () {
                        return undefined;
                    }
                }
            }).success(function(data){
                if(data['success']===true){
                    //prompt("文件上传成功");
                }else{
                    //prompt("文件上传失败");
                }
            }).error(function(){
                //prompt("文件上传失败");
            });
        }else{
            console.log("没有选择文件")
        }
        var postData = {title:title,showImg:showImg,simIntro:simIntro,content:content,loveNum:0,url:url,kind:kind,articleId:articleId};

        if(title&&simIntro&&content&&articleId){
            if(articleIdArr.indexOf(articleId) !== -1){
                alert('articleId已存在');
                return 0;
            }
            $http({
                "url":"/postP",
                method:"post",
                data:postData
            }).success(function(data){
                console.log(data);
                if(data['success']===true){
                    //prompt("文章上传成功");
                }else{
                    //prompt("文章上传失败");
                }
            }).error(function(){
                //prompt("文章上传失败");
            });
        }else{
            console.log("有部分没填");
        }

    };
});
app.controller('postEdit',function($scope,$http){
    $http({
        url:"/getAll?edit=true",
        method:"get"
    }).success(function(data){
        $scope.posts = data['posts'];
        for(var i=0;i<$scope.posts.length;i++){
            articleIdArr.push($scope.posts[i]['articleId']);
        }
    });

    $scope.edit = function(post){
        $scope.editing_post = post;
    };
    $scope.update = function(){
        var articleId = "";
        var updateObj = {};
        for(var key in $scope.editing_post){
            if(key === "articleId"){
                articleId = $scope.editing_post[key];
            }else if(key !== "articleId"&&key!=="_id"){
                updateObj[key] = $scope.editing_post[key];
            }
        }
        console.log(updateObj);
        $http({
            url:"/updateP",
            method:"post",
            data:{articleId:articleId,updateObj:updateObj}
        }).success(function(data){
            if(data['success']){
                prompt("修改文章成功");
            }else{
                prompt("修改文章失败");
            }
        })
    };
    $scope.delete = function(post){
        var articleId = post.articleId;

        $http({
            url:"/deleteP",
            method:"post",
            data:{articleId:articleId}
        }).success(function(data){
            if(data['success']){
                $scope.posts.remove(post);
                prompt("删除文章成功");
            }else{
                prompt("删除文章失败");
            }
        })
    }
});

app.controller('commentEdit', function ($scope, $http) {
    //setTimeout(function(){
    //    $http({
    //        url:"/getAllComments",
    //        method:"get"
    //    }).success(function(data){
    //        $scope.comments = data['comments'];
    //    });
    //},1000);
    $http({
        url:"/getAllComments",
        method:"get"
    }).success(function(data){
        console.log(data);
        $scope.comments = data['comments'];
    });

    $scope.delete = function(comment){
        var _id = comment._id;
        console.log(_id);
        $http({
            url:"/deleteC",
            method:"post",
            data:{_id:_id}
        }).success(function(data){
            if(data['success']){
                $scope.comments.remove(comment);
                prompt("删除评论成功");
            }else{
                prompt("删除评论失败");
            }
        })
    }
});